home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / Miro_Downloader.exe / imageresize.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-11-12  |  2.2 KB  |  66 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. '''Code to handle resizing images.  '''
  5. import logging
  6. import os
  7. import traceback
  8. from platformutils import resizeImage
  9.  
  10. def _resizedKey(width, height):
  11.     return u'%sx%s' % (width, height)
  12.  
  13.  
  14. def _makeResizedPath(filename, width, height):
  15.     (path, ext) = os.path.splitext(filename)
  16.     path += '.%sx%s' % (width, height)
  17.     return path + ext
  18.  
  19.  
  20. def multiResizeImage(source_filename, sizes):
  21.     '''Resize an image to several sizes.
  22.  
  23.     Arguments:
  24.         source_filename -- image to resize
  25.         sizes -- list of (width, height) tuples to resize to.
  26.     
  27.     Returns a dict storing the images successfully resized.  The keys are
  28.     "<width>x<height>" and the values are the paths to the image.
  29.     '''
  30.     results = { }
  31.     for width, height in sizes:
  32.         resizedPath = _makeResizedPath(source_filename, width, height)
  33.         
  34.         try:
  35.             resizeImage(source_filename, resizedPath, width, height)
  36.         except:
  37.             logging.warn('Error resizing %s to %sx%s:\n%s', source_filename, width, height, traceback.format_exc())
  38.             continue
  39.  
  40.         results[_resizedKey(width, height)] = resizedPath
  41.     
  42.     return results
  43.  
  44.  
  45. def getImage(resized_filenames, width, height):
  46.     """Fetch a image from the results of multiResizeImage().  If (width,
  47.     height) wasn't one of the combinations passed to multiResizeImage(), or
  48.     the image wasn't successfully resized, a KeyError will be thrown.
  49.     """
  50.     return resized_filenames[_resizedKey(width, height)]
  51.  
  52.  
  53. def removeResizedFiles(resized_filenames):
  54.     '''Delete the files returned by multiResizeImage().'''
  55.     for filename in resized_filenames.values():
  56.         
  57.         try:
  58.             if os.path.exists(filename):
  59.                 os.remove(filename)
  60.         continue
  61.         logging.warn('Error deleted resized image: %s\n%s', filename, traceback.format_exc())
  62.         continue
  63.  
  64.     
  65.  
  66.